package com.ibm.spatiotemp.server.framework.processor;

import org.apache.log4j.Logger;

import com.ibm.spatiotemp.server.constants.Constant;
import com.ibm.spatiotemp.server.datasources.DataSource;
import com.ibm.spatiotemp.server.framework.processor.dynamodb.DynamodbRawDataProcessor;
import com.ibm.spatiotemp.server.framework.processor.dynamodb.DynamodbSpatialQueryProcessor;
import com.ibm.spatiotemp.server.framework.processor.fake.FakeRawDataProcessor;
import com.ibm.spatiotemp.server.framework.processor.fake.FakeSpatialQueryProcessor;
import com.ibm.spatiotemp.server.framework.processor.geoindexdb.GeoIndexIndexBuildProcessor;
import com.ibm.spatiotemp.server.framework.processor.geoindexdb.GeoIndexSpatialQueryProcessor;
import com.ibm.spatiotemp.server.framework.processor.ifx.InformixRawDataProcessor;
import com.ibm.spatiotemp.server.framework.processor.ifx.InformixSpatialQueryProcessor;
import com.ibm.spatiotemp.server.framework.processor.mocking.MockingRawDataProcessor;
import com.ibm.spatiotemp.server.framework.processor.mocking.MockingSpatialQueryProcessor;
import com.ibm.spatiotemp.server.framework.processor.mysql.MysqlRawDataProcessor;
import com.ibm.spatiotemp.server.framework.processor.mysql.MysqlSpatialQueryProcessor;

public class ProcessorFactory {

	public static Logger logger = Logger.getLogger(ProcessorFactory.class);
	
	public static IndexBuildingProcessor getIndexBuildingProcessorInstance(DataSource dsrc){
//		if(Constant.DB_CLASS_IFX.equalsIgnoreCase(dsrc.getSource_class()))
//		{
//			logger.info("create data IndexBuildingProcessor " + Constant.DB_CLASS_IFX);
//			return new InformixIndexBuildingProcessor(dsrc);
//		}
//		else if(Constant.DB_CLASS_MYSQL.equalsIgnoreCase(dsrc.getSource_class()))
//			{
//			logger.info("create data IndexBuildingProcessor " + Constant.DB_CLASS_MYSQL);
//				return new MysqlIndexBuildingProcessor(dsrc);
//			}
//		else if(Constant.DB_CLASS_MOCK.equalsIgnoreCase(dsrc.getSource_class()))
//		{
//			logger.info("create data IndexBuildingProcessor " + Constant.DB_CLASS_MOCK);
//			return new MockingIndexBuildingProcessor(dsrc);
//		}
//		else if(Constant.DB_CLASS_FAKE.equalsIgnoreCase(dsrc.getSource_class()))
//		{
//			logger.info("create data IndexBuildingProcessor " + Constant.DB_CLASS_FAKE);
//			return new FakeIndexBuildingProcessor(dsrc);
//		}
//		else if(Constant.DB_CLASS_DYNAMODB.equalsIgnoreCase(dsrc.getSource_class()))
//		{
//			logger.info("create data IndexBuildingProcessor " + Constant.DB_CLASS_DYNAMODB);
//			return new DynamodbIndexProcessor(dsrc);
//		}
		return new GeoIndexIndexBuildProcessor(dsrc);
	}
	
	public static SpatialQueryProcessor getSpatialQueryProcessorInstance(DataSource dsrc){
		switch(dsrc.getAccessmode())
		{
		case Constant.DBACCESSMODE_DATAONLY:
		{
			logger.info("DBACCESSMODE_DATAONLY mode");
			return getSpatialQueryProcessorInstance_Internal(dsrc);
		}
		case Constant.DBACCESSMODE_IDXONLY:
		{
			logger.info("DBACCESSMODE_IDXONLY mode");
//			return getSpatialQueryProcessorInstance_Internal(dsrc);
			return new GeoIndexSpatialQueryProcessor(dsrc);
		}
		case Constant.DBACCESSMODE_IDXDATA:
		{
			logger.info("DBACCESSMODE_IDXDATA mode");
//			return getSpatialQueryProcessorInstance_Internal(dsrc);
			return new GeoIndexSpatialQueryProcessor(dsrc);
		}
		}
		
		return null;
	}

	/**
	 * here: getSpatialQueryProcessorInstance_Internal return origianal spatialqueryprocessor without help of geoindexdb
	 * @see ProcessorFactory#getSpatialQueryProcessorInstance(DataSource)
	 * @param dsrc
	 * @return
	 */
	private static SpatialQueryProcessor getSpatialQueryProcessorInstance_Internal(
			DataSource dsrc) {
		if(Constant.DB_CLASS_IFX.equalsIgnoreCase(dsrc.getSource_class()))
		{
			logger.info("create data SpatialQueryProcessor " + Constant.DB_CLASS_IFX);
			return new InformixSpatialQueryProcessor(dsrc);
		}
		else if(Constant.DB_CLASS_MYSQL.equalsIgnoreCase(dsrc.getSource_class()))
			{
			logger.info("create data SpatialQueryProcessor " + Constant.DB_CLASS_MYSQL);
				return new MysqlSpatialQueryProcessor(dsrc);
			}
		else if(Constant.DB_CLASS_MOCK.equalsIgnoreCase(dsrc.getSource_class()))
		{
			logger.info("create data SpatialQueryProcessor " + Constant.DB_CLASS_MOCK);
			return new MockingSpatialQueryProcessor(dsrc);
		}
		else if(Constant.DB_CLASS_FAKE.equalsIgnoreCase(dsrc.getSource_class()))
		{
			logger.info("create data SpatialQueryProcessor " + Constant.DB_CLASS_FAKE);
			return new FakeSpatialQueryProcessor(dsrc);
		}
		else if(Constant.DB_CLASS_DYNAMODB.equalsIgnoreCase(dsrc.getSource_class()))
		{
			logger.info("create data SpatialQueryProcessor " + Constant.DB_CLASS_DYNAMODB);
			return new DynamodbSpatialQueryProcessor(dsrc);
		}
		else
		{
			logger.error("unsupported SpatialQueryProcessor for " + dsrc.getConnection_string());
			return null;
		}
	}
	
	public static RawDataProcessor getRawDataProcessorInstance(DataSource dsrc){
		if(Constant.DB_CLASS_IFX.equalsIgnoreCase(dsrc.getSource_class()))
		{
			logger.info("create data RawDataProcessor " + Constant.DB_CLASS_IFX);
			return new InformixRawDataProcessor(dsrc);
		}
		else if(Constant.DB_CLASS_MYSQL.equalsIgnoreCase(dsrc.getSource_class()))
			{
			logger.info("create data RawDataProcessor " + Constant.DB_CLASS_MYSQL);
				return new MysqlRawDataProcessor(dsrc);
			}
		else if(Constant.DB_CLASS_MOCK.equalsIgnoreCase(dsrc.getSource_class()))
		{
			logger.info("create data RawDataProcessor " + Constant.DB_CLASS_MOCK);
			return new MockingRawDataProcessor(dsrc);
		}
		else if(Constant.DB_CLASS_FAKE.equalsIgnoreCase(dsrc.getSource_class()))
		{
			logger.info("create data RawDataProcessor " + Constant.DB_CLASS_FAKE);
			return new FakeRawDataProcessor(dsrc);
		}
		else if(Constant.DB_CLASS_DYNAMODB.equalsIgnoreCase(dsrc.getSource_class()))
		{
			logger.info("create data RawDataProcessor " + Constant.DB_CLASS_DYNAMODB);
			return new DynamodbRawDataProcessor(dsrc);
		}
		return null;
	}
	
}
